define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'service.order/index' + location.search,
                    add_url: 'service.order/add',
                    // edit_url: 'service.order/edit',
                    // del_url: 'service.order/del',
                    multi_url: 'service.order/multi',
                    import_url: 'service.order/import',
                    table: 'service_order',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'total_sn', title: __('Total_sn'), operate: 'LIKE'},
                        {field: 'service.title', title: __('Category.name'), operate: 'LIKE'},
                        {field: 'category.name', title: __('Category_id'), operate: 'LIKE'},
                        {field: 'total_amount', title: __('Total_amount'), operate:'BETWEEN'},
                        {field: 'pay_amount', title: __('Pay_amount'), operate:'BETWEEN'},
                        {field: 'deduction_amount', title: __('Deduction_amount'), operate:'BETWEEN'},
                        {field: 'coupon.name', title: __('Coupon_id')},
                        {field: 'jz.name', title: __('Name'), operate: 'LIKE'},
                        {field: 'actiontime', title: __('Actiontime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false},
                        // {field: 'hospital.title', title: __('Hospital.title'), operate: 'LIKE'},
                        {field: 'escort.realname', title: __('Escort_id'), operate: 'LIKE'},
                        {field: 'commission', title: __('Commission'), operate:'BETWEEN'},
                        {field: 'endtime', title: __('Endtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'servicetime', title: __('Servicetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'canceltime', title: __('Canceltime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'user.nickname', title: __('User_id')},
                        {field: 'status', title: __('Status'), searchList: {"0":__('Status 0'),"1":__('Status 1'),"5":__('Status 5'),"2":__('Status 2'),"3":__('Status 3'),"4":__('Status 4'),"-1":__('Status -1')}, formatter: Table.api.formatter.status},
                        {field: 'city.city', title: __('City_id')},
                        {field: 'hospital.address', title: __('Hospital.address'), operate: 'LIKE'},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate,
                        buttons: [
                            {
                                name: 'dispatch_order',
                                title: '派单',
                                text: '派单',
                                classname: 'btn btn-primary btn-xs btn-view btn-dialog',
                                url: 'service.user/index',//接口地址 控制器名/方法名
                                refresh: true,
                                visible: function (row) {
                                    console.log(row.status)
                                    if (row.status == 1) {
                                        return true;
                                    }else{
                                        return false;
                                    }
                                },
                            },
                            {
                                name: 'cancel',
                                title: '取消订单',
                                text: '取消订单',
                                classname: 'btn btn-primary btn-xs btn-view btn-dialog',
                                url: 'service.order/cancel',//接口地址 控制器名/方法名
                                refresh: true,
                                visible: function (row) {
                                    if (row.status == 1 || row.status == 2 || row.status == 3 || row.status == 5) {
                                        return true;
                                    }else{
                                        return false;
                                    }
                                },
                            },
                            {
                                name: 'transfer',
                                title: '转派',
                                text: '转派',
                                classname: 'btn btn-success btn-xs btn-view btn-dialog',
                                url: 'service.order/transfer',//接口地址 控制器名/方法名
                                refresh: true,
                                visible: function (row) {
                                    if (row.status == 1 || row.status == 2 || row.status == 3 || row.status == 5) {
                                        return true;
                                    }else{
                                        return false;
                                    }
                                },
                            },
                            {
                                name: 'view_order',
                                title: '查看订单',
                                text: '查看订单',
                                extend:'data-area=["1200px","800px"]',
                                classname: 'btn btn-primary btn-xs btn-view btn-dialog',
                                url: 'service.order/info',//接口地址 控制器名/方法名
                                refresh: true,
                               
                            },
                        ]}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        cancel: function () {
            Controller.api.bindevent();
        },
        transfer: function () {
            $("#c-escort_id").data("params", function (obj) {
                //obj为SelectPage对象
                return {custom: {city_id: $("#city_id").val()}};
            });
            Controller.api.bindevent();
        },
        dispatch_order:function(){
            console.log(111);
            Controller.api.bindevent();
        },
        api: {
            
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
